package com.lhkj.ct.admin.system.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.lhkj.ct.base.annntation.Log;
import com.lhkj.ct.base.constants.SystemConstants;
import com.lhkj.ct.base.enums.ActionType;
import com.lhkj.ct.base.model.ro.ActionResult;
import com.lhkj.ct.base.utils.Asserts;
import com.lhkj.ct.base.validator.group.ValGroups;
import com.lhkj.ct.meta.admin.model.dto.EmpPageQuery;
import com.lhkj.ct.meta.admin.model.entity.TblEmployee;
import com.lhkj.ct.meta.admin.model.vo.EmpPageVo;
import com.lhkj.ct.meta.admin.service.SysEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import java.text.MessageFormat;
import java.util.List;

/**
 * <p>
 *      TODO 员工管理
 * </p>
 *
 * @author jobob
 * @since 2023-08-23
 */
@RestController
@RequestMapping("api/system/employee")
@ApiSupport(order = 2)
@Api(tags = "02.系统用户")
//@RequiresRoles(value = {SystemConstants.ADMIN_ROLE_CODE})
public class SysEmployeeController {

    @Resource
    private SysEmployeeService sysEmployeeService;

    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "用户分页列表", notes = "员工分页查询")
    @GetMapping("list")
    ////@RequiresPermissions(value = "system:employee:list")
    public ActionResult<IPage<EmpPageVo>> list(EmpPageQuery pageQuery) {
        return ActionResult.success(sysEmployeeService.selectEmpPage( pageQuery));
    }

    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "用户表单数据", notes = "查看员工信息")
    @GetMapping("get/{id}")
    ////@RequiresPermissions(value = {"system:employee:edit", "system:employee:get"}, logical = Logical.OR)
    public ActionResult<?> get(@PathVariable @NotBlank(message = "菜单id不能为空") String id) {
        return ActionResult.success(sysEmployeeService.getById(id));
    }

    @ApiOperationSupport(order = 3)
    @PostMapping("insert")
    @ApiOperation(value = "新增", notes = "新增员工")
    @Log(title = "系统用户", actionType = ActionType.INSERT)
    ////@RequiresPermissions(value = "system:employee:insert")
    public ActionResult<Void> insert(@Validated(ValGroups.Insert.class) @RequestBody TblEmployee entity) {
        return ActionResult.judge(sysEmployeeService.save(entity));
    }

    @ApiOperationSupport(order = 4)
    @PutMapping("update")
    @ApiOperation(value = "编辑", notes = "编辑员工")
    @Log(title = "系统用户", actionType = ActionType.UPDATE)
    ////@RequiresPermissions(value = "system:employee:edit")
    public ActionResult<Void> update(@Validated(ValGroups.Update.class) @RequestBody TblEmployee entity) {
        return ActionResult.judge(sysEmployeeService.save(entity));
    }

    @ApiOperationSupport(order = 5)
    @DeleteMapping("delete")
    @ApiOperation(value = "批量删除", notes = "删除员工")
    @Log(title = "系统用户", actionType = ActionType.DELETE)
    ////@RequiresPermissions(value = "system:employee:delete")
    public ActionResult<Void> delete(@RequestBody @NotEmpty(message = "id不能为空") List<String> ids) {
        if (sysEmployeeService.removeByIds(ids) > 0) {
            return ActionResult.success("删除成功");
        }
        return ActionResult.fail("删除失败");
    }

    @ApiOperationSupport(order = 5)
    @PutMapping("{userId}/resetPwd")
    @ApiOperation(value = "重置密码")
    ////@RequiresPermissions(value = "system:employee:edit")
    public ActionResult<Void> resetPassword(@PathVariable String userId) {
        String initPassword = sysEmployeeService.resetPassword(userId);
        return ActionResult.success(MessageFormat.format("重置密码成功，新密码为:{0}", initPassword));
    }

    /**
     * 状态修改
     */
//    //@RequiresPermissions("@ss.hasPermi('system:user:edit')")
    @Log(title = "用户管理", actionType = ActionType.UPDATE)
    @PutMapping("/changeStatus")
    public ActionResult<Void> changeStatus(@RequestBody TblEmployee user) {
        TblEmployee target = sysEmployeeService.getById(user.getId());
        Asserts.assertFalse(null == target, "用户不存在或数据标识符无效");
        Asserts.assertFalse(SystemConstants.ROOT_LOGIN_NAME.equalsIgnoreCase(target.getEmpNo()), "不允许操作超级管理员用户");
        return ActionResult.judge(sysEmployeeService.updateUserStatus(user));
    }
}
